/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.cef.command.listener;

import cef.command.CommandI;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListener;
import cef.command.listener.CommandOutputListenerException;
import cef.command.listener.CommandPrintWriter;
import commons.util.PrintUtil;
import genomemap.cef.command.RepeatsMap_Cmd;

import java.util.Map;

/**
 * @version 1.3.0
 * @author Susanta Tewari
 * @history Created on 2/14/13
 * @since 1.3.0
 */
public class RepeatsMap_PW extends CommandPrintWriter {

    @Override
    public void receivedEventImpl(final CommandOutputEvent cmdOutputEvent)
            throws CommandOutputListenerException {

        final RepeatsMap_Cmd cmd = (RepeatsMap_Cmd) cmdOutputEvent.getSource();
        final Map<Integer, RepeatsMap_Cmd.RepeatCountVO> repeatCounts = cmd.getRepeatCounts();

        for (final Integer lg : repeatCounts.keySet()) {

            getPrintWriter().println("Linkage Group: " + lg);
            print_repeatCount(repeatCounts.get(lg));
            getPrintWriter().println();
        }
    }

    private void print_repeatCount(final RepeatsMap_Cmd.RepeatCountVO vo) {

        final String[][] result_1 = new String[2][3];

        result_1[0] = new String[] { "S", "R", "A" };

        final int[] counts = vo.getCounts();

        for (int i = 0; i < 3; i++) {
            result_1[1][i] = "" + counts[i];
        }

        getPrintWriter().println(PrintUtil.print2D(result_1, 2, 1, null));


        // result-2
        final int[][] counts2  = vo.getTransitionCounts();
        final String[][] table = new String[counts.length + 1][4];

        table[0]    = new String[] { "", "S", "R", "A" };    // row header
        table[1][0] = "S";
        table[2][0] = "R";
        table[3][0] = "A";

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {
                table[i + 1][j + 1] = "" + counts2[i][j];
            }
        }

        getPrintWriter().println(PrintUtil.print2D(table, 2, 1, null));
    }

    @Override
    public CommandOutputListener newInstance() {
        return new RepeatsMap_PW();
    }

    @Override
    public Class<? extends CommandI> getTargetCommandClass() {
        return RepeatsMap_Cmd.class;
    }
}
